/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年4月14日         luwei
 */

package com.coocaa.x.framework.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import java.util.HashMap;

public class NativeImageCache {
    private HashMap<Object, SmartBitmap> mBitmapCache = new HashMap<Object, SmartBitmap>();
    private static NativeImageCache mInstance = null;

    private NativeImageCache() {
    }

    public static synchronized NativeImageCache getInstance() {
        if (mInstance == null) {
            mInstance = new NativeImageCache();
        }
        return mInstance;
    }

    public synchronized SmartBitmap getBitmapById(Resources resource, int id, int width, int height) {
        LogUtils.d("luwei", "get bitmap id " + id);
        if (mBitmapCache.get(id) != null) {
            mBitmapCache.get(id).addCount();
            return mBitmapCache.get(id);
        } else {
            Bitmap bitmap = ImageUtils.decodeSampledBitmapFromResource(resource, id, width, height);
            if (bitmap.getWidth() > width) {
                bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
            }
            if (bitmap == null) {
                return null;
            }
            mBitmapCache.put(id, new SmartBitmap(bitmap, id));
            return mBitmapCache.get(id);
        }
    }

//    public synchronized SmartBitmap getBitmapByActivity();

    public synchronized SmartBitmap getBitmapByPkg(Context context, String pkg, String activity,
                                                   int width, int height,int corner) {
        LogUtils.d("luwei", "get pkg is " + pkg);
        String key = pkg;
        if (activity != null && !activity.equals("")) {
            key = pkg + ":" + activity;
        }
        if (mBitmapCache.get(key) != null) {
            mBitmapCache.get(key).addCount();
            return mBitmapCache.get(key);
        } else {
            PackageInfo info;
            try {
                info = context.getPackageManager().getPackageInfo(pkg, 0);
                Drawable d = null;
                if (pkg.equals("com.tianci.appstore") && (activity != null && !activity.equals(""))) {
                    ComponentName easyLauncher = new ComponentName(context.getPackageName(), activity);
                    d = context.getPackageManager().getActivityIcon(easyLauncher);
                } else {
                    d = info.applicationInfo.loadIcon(context.getPackageManager());
                }
                if (d != null) {
                    Bitmap bm = ((BitmapDrawable) d).getBitmap();
                    if (bm.getWidth() > width) {
                        bm = ImageUtils.scale(bm, width, height);
                    }
                    if (corner > 0)
                        bm = ImageUtils.getRoundedCornerBitmap(bm,corner);
                    SmartBitmap smartBitmap = new SmartBitmap(bm, key);
                    mBitmapCache.put(key, smartBitmap);
                    return smartBitmap;
                }
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public synchronized SmartBitmap getBitmapByPkg(Context context, String pkg, String activity,
                                                   int width, int height){
        return getBitmapByPkg(context,pkg,activity,width,height,0);
    }

    public synchronized SmartBitmap getBitmapByPath(String path, int width, int height) {
        LogUtils.d("luwei", "get path is " + path);
        if (mBitmapCache.get(path) != null) {
            mBitmapCache.get(path).addCount();
            return mBitmapCache.get(path);
        } else {
            Bitmap bitmap = ImageUtils.decodeSampledBitmapFromFile(path, width, height);
            if (bitmap.getWidth() > width) {
                bitmap = ImageUtils.scale(bitmap, width, height);
            }
            SmartBitmap result = new SmartBitmap(bitmap, path);
            mBitmapCache.put(path, result);
            return result;
        }
    }

    public class SmartBitmap {
        private int mCount = 1;
        private Bitmap mBitmap = null;
        private Object mKey = null;

        public SmartBitmap(Bitmap bitmap, Object key) {
            this.mBitmap = bitmap;
            this.mKey = key;
        }

        public synchronized Bitmap get() {
            return mBitmap;
        }

        protected synchronized void addCount() {
            mCount++;
        }

        public synchronized void recycle() {
            synchronized (NativeImageCache.this) {
                mCount--;
//                if (mCount == 0)
//                {
//                    mBitmap.recycle();
//                    mBitmap = null;
//                    mBitmapCache.remove(mKey);
//                }
            }
        }
    }
}
